package com.example.change2;

import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.change2.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SecondActivety extends AppCompatActivity {
    private EditText medittext;
    private ImageButton mimagebutton;

    private RadioGroup mRadioGroup;
    private RadioButton mRadioBUutton1;
    private RadioButton mRadioBUutton2;
    private RadioButton mRadioBUutton3;
    private RadioButton mRadioBUutton4;
    private CheckBox mcheckbox1;
    private CheckBox mcheckbox2;
    private CheckBox mcheckbox3;

    private Switch mSwitch;

    private ProgressBar mprogressBar;
    private Button progress_button;
    private int progress;
    private int secondProgress;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("TAG", "====onClick: second2=========");
//        List list = new ArrayList();
//        Log.d("TAG", "--------------second2-----崩溃 ");
        setContentView(R.layout.activity_second);
        medittext = findViewById(R.id.edittext);


        mimagebutton = findViewById(R.id.imagebutton);
        mimagebutton.setVisibility(View.GONE);




        mRadioGroup = findViewById(R.id.radiogroup);
        mRadioBUutton1 = findViewById(R.id.rb1);
        mRadioBUutton2 = findViewById(R.id.rb2);
        mRadioBUutton3 = findViewById(R.id.rb3);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mRadioBUutton3.isChecked()) {
                    mRadioBUutton3.setBackground(getDrawable(R.mipmap.dp));
                    mRadioBUutton3.setTextColor(Color.parseColor("#ff4562"));
                } else {
                    mRadioBUutton3.setBackground(getDrawable(R.mipmap.dp_with));
                    mRadioBUutton3.setTextColor(Color.parseColor("#454562"));
                }
                if (mRadioBUutton1.isChecked()) {
                    mRadioBUutton1.setButtonDrawable(getDrawable(R.mipmap.dp));
                } else {
                    mRadioBUutton1.setButtonDrawable(getDrawable(R.mipmap.bg_delete_pressed));
                }
            }
        });


        mimagebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                medittext.setText("");
            }
        });
        medittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            mimagebutton.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE);
            }
        });


        mcheckbox1 = findViewById(R.id.cd1);
        mcheckbox2 = findViewById(R.id.cd2);
        mcheckbox3 = findViewById(R.id.cd3);
        mcheckbox1.setChecked(true);
        mcheckbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    mcheckbox1.setBackground(getDrawable(R.mipmap.dp));
                }else {
                    mcheckbox1.setBackground(getDrawable(R.mipmap.dp_with));
                }
            }
        });
        mcheckbox2.setButtonDrawable(getDrawable(R.drawable.bd_delete_selector));
        mcheckbox3.setBackgroundResource(R.drawable.button_bg_selector);

        mSwitch = findViewById(R.id.sw1);
        mSwitch.setThumbDrawable(getDrawable(R.mipmap.db_delete));
        mSwitch.setTrackDrawable(getDrawable(R.mipmap.bg_checked));

        mSwitch.setTextOn("On");
        mSwitch.setTextOff("Off");

        mSwitch.setShowText(false);

        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtils.d("TAG","当前开关状态为："+isChecked);
            }
        });
        mprogressBar = findViewById(R.id.progress);
        progress_button = findViewById(R.id.progress_botton);

        progress = mprogressBar.getProgress();
        secondProgress = mprogressBar.getSecondaryProgress();
        progress_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mprogressBar.setProgress(++progress);
                mprogressBar.setSecondaryProgress(++secondProgress);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progress < mprogressBar.getMax()){
                            mprogressBar.setProgress(++progress);
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                });
            }
        });
    }
}
